<template>
  <div id="dialogResetPass">
    <el-dialog
      title="重置密码"
      :visible.sync="dialogData.isShow"
      :before-close="labelHandleClose"
      width="450px"
    >
      <div class="form-box">
        <el-form
          ref="ruleForm"
          :model="form"
          :rules="formRules"
          label-width="90px"
        >
          <el-form-item label="登录名称" prop="loginName">
            <el-input v-model="form.loginName" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="输入密码" prop="password">
            <el-input
              v-model.trim="form.password"
              placeholder="请输入密码"
              show-password
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="success" plain @click="cancelLabel">
          取 消
        </el-button>
        <el-button
          type="success"
          :loading="loading"
          @click="confirmLabel('ruleForm')"
        >
          确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { resetPass } from '@/api/system/user' // 接口
import { getKeyValueValue } from '@/api/system/parameter'
import { sysNormalDisable } from '@utils/dictionariesFunc'
export default {
  name: 'dialogResetPass',
  msg: '用户管理-重置密码',
  props: ['dialogData'],
  data() {
    return {
      loading: false,
      // 表单数据
      form: {
        userId: null,
        loginName: '', // 登录名称
        password: '' // 输入密码
      },
      //表单校验格式
      formRules: {
        loginName: {
          required: true,
          message: '请输入登录名称',
          trigger: 'blur'
        },
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, message: '最少5个字符', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.form.userId = this.dialogData.userId
    this.form.loginName = this.dialogData.loginName
    this.getInitPass()
  },
  methods: {
    /* 获取初始化密码 */
    async getInitPass() {
      let obj = {
        configKey: 'sys.user.initPassword'
      }
      await getKeyValue(obj, (response) => {
        this.form.password = String(response)
      })
    },
    labelHandleClose(done) {
      this.cancelLabel()
      done()
    },
    cancelLabel() {
      // 关闭前清空
      this.loading = false
      this.dialogData.isShow = false
      this.dialogData.userId = null
      this.dialogData.loginName = null
    },
    /* 确定 */
    confirmLabel(formName) {
      let _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          _this.subForm()
        } else {
          return false
        }
      })
    },
    /* 提交表单 */
    async subForm() {
      this.loading = true
      let obj = { ...this.form }
      await resetPass(
        this.form.userId,
        obj,
        (response) => {
          this.$message.success('重置密码成功')
          this.loading = false
          /* this.$emit('refresh')*/
          this.cancelLabel()
        },
        (response) => {
          this.loading = false
          this.$message.warning('重置密码失败')
        }
      )
    }
  }
}
</script>
<style lang="less">
#dialogResetPass {
  .el-dialog .el-dialog__body {
    margin-bottom: 25px;
  }
}
</style>
